package com.gml.jdk.proxy.dynamic;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName : ProxyGeneratorUtils
 * @Description : 将动态代理对象字节码写入到磁盘
 * @Author : gml
 * @Date: 2020-10-20 17:20
 */
public class ProxyGeneratorUtils {

    public static void writeProxyClassToDisk(String path){
        byte[] generateProxyClass = ProxyGenerator.generateProxyClass("$Proxy11", Hello.class.getInterfaces());

        FileOutputStream out = null;

        try {
            out = new FileOutputStream(path);
            out.write(generateProxyClass);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ProxyGeneratorUtils.writeProxyClassToDisk("D:\\$Proxy11.class");
    }
}
